package com.zq.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateSessionFactory {

	private static Configuration configuration = new Configuration();
	private static ServiceRegistry serviceRegistry;
	private static SessionFactory sessionFactory;

	private HibernateSessionFactory() {
	}

	static {
		configuration.configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
				.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	}

	public static Session getSession() {
			if (sessionFactory == null) {
				sessionFactory = rebuildSessionFactory();
			}
		return sessionFactory.getCurrentSession();
	}
	
	

	public static SessionFactory rebuildSessionFactory() {
		configuration.configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
				.buildServiceRegistry();
		return configuration.buildSessionFactory(serviceRegistry);
	}

	
	// 5-������
	public static Configuration getConfiguration() {
		return configuration;
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

}